<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://xiaoji.name All rights reserved.
// +----------------------------------------------------------------------
// | Author: jackie <297129853@qq.com>
// +----------------------------------------------------------------------
namespace plugins\zthy_sms;

use cmf\lib\Plugin;
use think\Db;

class ZthySmsPlugin extends Plugin
{
    public $info = array(
        'name'=>'ZthySms',
        'title'=>'助通短信',
        'description'=>'助通短信',
        'status'=>1,
        'author'=>'',
        'version'=>'1.0'
    );
    
    public $has_admin=0;//插件是否有后台管理界面
    
    public function install()
    {//安装方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '助通短信',
            'hook' => 'send_zthy_sms',
            'app' => 'cmf',
            'description' => '助通短信'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (empty($test)) {
            Db::name('hook')->insert($where_data1);
        }
        return true;//安装成功返回true，失败false
    }
    
    public function uninstall()
    {//卸载方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '助通短信',
            'hook' => 'send_zthy_sms',
            'app' => 'cmf',
            'description' => '助通短信'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (!empty($test)) {
            Db::name('hook')->where($where_data1)->delete();
        }
        return true;//卸载成功返回true，失败false
    }

    public function sendZthySms($param)
    {
        //模板信息发送demo
        $url     = "https://api-shss.zthysms.com/v2/sendSmsTp";
        $config        = $this->getConfig();

        foreach ($config as $key => $value) {
            if(empty($value)){
                return ['code'=>'0','msg'=>'短信参数未配置'];
            }
        }

        $records[] = ['mobile'=>$param['mobile'],'tpContent'=>['valid_code'=>$param['code']]];
        $tKey     = time();
        $password = md5(md5($config['password']) . $tKey);
        $date     = array(
            'tpId'      => $config['tpId'], //模板id
            'username'  => $config['username'], //用户名
            'password'  => $password, //密码
            'tKey'      => $tKey, //tKey
            'signature' => '【'.$config['signature'].'】',
            'records'   => $records
        );

        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($date)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HEADER, false); //开启header
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8'
        )); //类型为json
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Error POST' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return json_decode($result,true);
    }

}